<template>
	<view class="content">
		<!-- 这里设置了z-paging加载时禁止自动调用reload方法，自行控制何时reload（懒加载）-->
		<z-paging ref="paging" @query="queryList" :list.sync="dataList" :mounted-auto-call-reload="false" style="height: 100%;">
			<!-- 如果希望其他view跟着页面滚动，可以放在z-paging标签内 -->
			<!-- list数据，建议像下方这样在item外层套一个view，而非直接for循环item，因为slot插入有数量限制 -->
			<view class="itemContent">
				<view class="item" v-for="(item,index) in dataList" :key="index">
					<view class="item-top-content">
						<text class="item-top-content-date">{{item.ptReciveTime}}</text>
						<text class="item-common-text">送达</text>
						<text class="item-top-content-money">{{item.amount}}</text>
						<text class="item-top-content-com">￥</text>
					</view>
					<view class="item-address">
						<view class="item-address-left">
							<text class="item-address-top-dist">800m</text>
							<text class="item-address-common-text">送货</text>
						</view>
						<view class="item-address-right">
							<text class="item-address-right-info">{{item.address}}</text>
							<text class="item-address-right-user" @click="callTel(item.contactPhone)">{{item.contacts}}  {{item.contactPhone}}</text>
						</view>
					</view>
					<view v-if="item.orderGoodsInfo.length > 0">
						<view class="item-goods"  v-for="items in item.orderGoodsInfo" :key="index" >
							<view class="item-goods-business">{{items.shopName}}</view>
							<view class="item-goods-content">
								<view class="item-goods-item" v-for="item_goods in items.goodsInfoList">
									<text class="item-goods-name">{{item_goods.goodsName}}</text>
									<text class="item-goods-num">x{{item_goods.goodsNumber}}</text>
									<text class="item-goods-money">￥{{item_goods.amount}}</text>
								</view>
							</view>
							<view class="item-orders" v-if="showArray[index]">
								<view class="item-goods-business">订单详情</view>
								<view class="item-orders-line">
									<view class="item-orders-line-title">订单号码</view>
									<view class="item-orders-line-value">{{item.orderNo}}</view>
								</view>
								<view class="item-orders-line">
									<view class="item-orders-line-title">顾客下单时间</view>
									<view class="item-orders-line-value">{{item.orderDate}}</view>
								</view>
							</view>
							<view class="show-item-orders" v-if="showArray[index]" @click="hideOrderInfo(index)">
								<view class="show-item-orders-name">收起详情</view>
								<image src="../../static/up.png" mode="aspectFill" class="s_img"></image>
							</view>
							<view class="show-item-orders" v-else @click="showOrderInfo(index)">
								<view class="show-item-orders-name">展开详情</view>
								<image src="../../static/down1.png" mode="aspectFill" class="s_img"></image>
							</view>
						</view>
					</view>
					<view v-else>
						<view class="item-goods">
							<view class="item-goods-business" v-if="item.taskType == 0">请求帮助项</view>
							<view class="item-goods-business" v-if="item.taskType == 1">帮忙送货</view>
							<view class="item-goods-business" v-if="item.taskType == 2">帮忙取货</view>
							<view class="item-goods-content">
								<view class="item-goods-item noOrderGoods">
									<text class="item-goods-name noOrderGoodsName">{{item.customOrderInfo}}</text>
								</view>
							</view>
							<view class="item-orders" v-if="showArray[index]">
								<view class="item-goods-business">订单详情</view>
								<view class="item-orders-line" v-if="item.taskType == 1">
									<view class="item-orders-line-title">送达地址</view>
									<view class="item-orders-line-value">{{item.contactInfo.addr}}</view>
								</view>
								<view class="item-orders-line" v-if="item.taskType == 2">
									<view class="item-orders-line-title">取货地址</view>
									<view class="item-orders-line-value">{{item.contactInfo.addr}}</view>
								</view>
								<view class="item-orders-line" v-if="item.taskType >= 1">
									<view class="item-orders-line-title">联系人</view>
									<view class="item-orders-line-value">{{item.contactInfo.name}}</view>
								</view>
								<view class="item-orders-line" v-if="item.taskType >= 1">
									<view class="item-orders-line-title">联系电话</view>
									<view class="item-orders-line-value" style="color: #007AFF;font-weight: 700;" @click="callTel(item.contactInfo.phone)">{{item.contactInfo.phone}}</view>
								</view>
								
								<view class="item-orders-line">
									<view class="item-orders-line-title">订单号码</view>
									<view class="item-orders-line-value">{{item.orderNo}}</view>
								</view>
								<view class="item-orders-line">
									<view class="item-orders-line-title">顾客下单时间</view>
									<view class="item-orders-line-value">{{item.orderDate}}</view>
								</view>
							</view>
							<view class="show-item-orders" v-if="showArray[index]" @click="hideOrderInfo(index)">
								<view class="show-item-orders-name">收起详情</view>
								<image src="../../static/up.png" mode="aspectFill" class="s_img"></image>
							</view>
							<view class="show-item-orders" v-else @click="showOrderInfo(index)">
								<view class="show-item-orders-name">展开详情</view>
								<image src="../../static/down1.png" mode="aspectFill" class="s_img"></image>
							</view>
						</view>
					</view>
					
					<view class="item-bottom-btn">
						<view class="item-btn-retreat" @click="backOrder(item)" v-if="currentIndex == 0">退单</view>
						<view class="item-btn-rob" @click="robOrder(item)" v-if="currentIndex == 0">抢单</view>
					</view>
				</view>
			</view>
		</z-paging>
	</view>
</template>

<script>
	import base from '@/api/base'
	export default {
		data() {
			return {
				dataList: [],
				firstLoaded: false,
				timer:null,
				times:0,//定时加载的次数
				showArray:[],
				flush:false
			}
		},
		props:{
			tabIndex: {
				type: Number,
				default: function(){
					return 0
				}
			},
			currentIndex: {
				type: Number,
				default: function(){
					return 0
				}
			},
		},
		watch: {
			currentIndex: {
				handler(newVal) {
					if(newVal === this.tabIndex){
						//懒加载，当滑动到当前的item时，才去加载
						// if(!this.firstLoaded){
							this.$nextTick(()=>{
								this.$refs.paging.reload();
							})
						// }
					}
				},
				immediate: true
			},
		},
		
		methods: {
			queryList(pageNo, pageSize) {
				let that = this
				//组件加载时会自动触发此方法，因此默认页面加载时会自动触发，无需手动调用
				//这里的pageNo和pageSize会自动计算好，直接传给服务器即可
				//模拟请求服务器获取分页数据，请替换成自己的网络请求
				if(that.currentIndex == 0 && that.times != 0){
					console.log(this.currentIndex)
					// that.timer = setInterval(() =>{
						this.$request.queryOrderList(pageNo, pageSize, that.currentIndex + 1, (data) => {
							this.$refs.paging.complete(data);
							that.firstLoaded = true;
						})
					// },5000)
				}else{
					console.log(this.currentIndex)
					that.times += 1
					if(this.currentIndex != 0){
						that.times = 0
					}
					// clearInterval(that.timer);
					that.timer = null;  
					this.$request.queryOrderList(pageNo, pageSize, this.currentIndex + 1, (data) => {
						this.$refs.paging.complete(data);
						that.firstLoaded = true;
					})
				}
				
				
			},
			showOrderInfo(index){
				let that = this
				that.showArray[index] = true
				that.flush = !that.flush
			},
			hideOrderInfo(index){
				let that = this
				that.showArray[index] = false
				that.flush = !that.flush
			},
			itemClick(item) {
				console.log('点击了', item.title);
			},
			backOrder(item){
				this.$emit("backOrder",item)
			},
			callTel(phone){
				console.log(phone)
				uni.makePhoneCall({
				    phoneNumber: phone
				});
			},
			robOrder(item){
				let that = this
				
				let postData 	=	{orderNo:item.orderNo}
				this.$api.order.robOrder(item)
				.then(res => {
					if(res.code == 200){
						// this.$commonJs.showToast.showNoneToast(res.msg)
						// this.$commonJs.showToast.showToasting(res.msg + "数据加载中")
						uni.showLoading({
							title: res.msg
						});
						
						setTimeout(function () {
							uni.hideLoading();
							that.queryList(base.common_pageNum,base.common_pageSize)
						}, 2000);
						
					}else{
						this.$commonJs.showToast.showNoneToast(res.msg)
					}
				})
				.catch(err => {
					this.$commonJs.showToast.showNoneToast(err)
				})
			}
		}
	}
</script>

<style>
	/* 注意，1、父节点需要固定高度，z-paging的height:100%才会生效 */
	/* 注意，2、请确保z-paging与同级的其他view的总高度不得超过屏幕宽度，以避免超出屏幕高度时页面的滚动与z-paging内部的滚动冲突 */
	.content {
		height: 100%;
		/* 父节点建议开启flex布局 */
		display: flex;
		flex-direction: column;
	}
	.itemContent{
		background-color: #F5F5F5;
	}
	.item {
		width: 702rpx;
		position: relative;
		display: inline-table;
		align-items: center;
		justify-content: space-between;
		margin: 0 24rpx 16rpx 24rpx;
		background-color: #FFFFFF;
		border-radius: 24rpx;
	}
	.item-top-content{
		width: 630rpx;
		padding: 0 36rpx 0 36rpx;
		height: 80rpx;
		border-bottom: 1rpx solid #EEEEEE;
		float: left;
		line-height: 80rpx;
	}
	.item-top-content .item-top-content-date{
		color: #527FFF;
		font-size: 28rpx;
		display: block;
		float: left;
	}
	.item-top-content .item-common-text{
		display: block;
		float: left;
		font-size: 28rpx;
		color: #191B1E;
		margin-left: 8rpx;
	}
	.item-top-content  .item-top-content-money{
		display: block;
		float: right;
		color: #191B1E;
		font-size: 36rpx;
		font-weight: 700;
	}
	.item-top-content  .item-top-content-com{
		font-size: 28rpx;
		display: block;
		float: right;
		color: #191B1E;
		font-weight: 700;
		margin-top: 4rpx;
		margin-right: 4rpx;
	}
	.item-address{
		width: 702rpx;
		float: left;
		/* height: 124rpx; */
		padding-bottom: 10rpx;
	}
	.item-address .item-address-left{
		width: 64rpx;
		float: left;
		height: 124rpx;
		margin-left: 32rpx;
		text-align: center;
	}
	.item-address .item-address-left .item-address-top-dist{
		width: 100%;
		float: left;
		display: block;
		color: #191B1E;
		font-size: 24rpx;
		margin-top: 34rpx;
	}
	.item-address .item-address-left .item-address-common-text{
		width: 100%;
		float: left;
		display: block;
		color: #999999;
		font-size: 22rpx;
	}
	.item-address .item-address-right{
		width: 542rpx;
		/* height: 124rpx; */
		margin-left: 18rpx;
		float: left;
	}
	.item-address .item-address-right .item-address-right-info{
		display: block;
		margin-top: 16rpx;
		float: left;
		color: #191B1E;
		font-size: 32rpx;
		font-weight: 700;
		width: 100%;
	}
	.item-address .item-address-right .item-address-right-user{
		display: block;
		color: #999999;
		font-size: 24rpx;
		margin-top: 14rpx;
		float: left;
	}
	.item-goods{
		width: 658rpx;
		float: left;
		background-color: #F8F8F8;
		border-radius: 12rpx;
		margin-left: 22rpx;
		margin-bottom: 26rpx;
	}
	.item-goods .item-goods-business{
		color: #191B1E;
		font-size: 30rpx;
		display: block;
		float: left;
		width: 100%;
		margin: 14rpx 0 10rpx 32rpx ;
		font-weight: 700;
	}
	.item-goods .item-goods-content{
		width: 100%;
		float: left;
		border-bottom: 1rpx solid #E1E1E1;
		padding-bottom: 20rpx;
	}
	.item-goods .item-goods-content .item-goods-item{
		float: left;
		width: 100%;
		margin: 0 32rpx 8rpx 32rpx;
		color: #999999;
		font-size: 28rpx;
	}
	
	.item-goods .item-goods-content .item-goods-item .item-goods-name{
		display: block;
		float: left;
		width: 392rpx;
		text-align: left;
	}
	.item-goods .item-goods-content .item-goods-item .item-goods-num{
		width: 70rpx;
		display: block;
		float: left;
		text-align: left;
	}
	.item-goods .item-goods-content .item-goods-item .item-goods-money{
		width: 112rpx;
		display: block;
		float: left;
		text-align: right;
	}
	.seeMore{
		width: 658rpx;
		float: left;
		margin-top: 10rpx;
		border-top: 1px solid #E1E1E1;
	}
	.seeMore .seeMore-title{
		font-size: 28rpx;
		color: #999999;
		line-height: 64rpx;
		display: block;
		float: left;
		margin: 12rpx 0 0 256rpx;
	}
	.seeMore .seeMore-img{
		width: 28rpx;
		height: 28rpx;
		float: left;
		display: block;
		margin: 28rpx 0 0 8rpx;
	}
	.item-bottom-btn{
		height: 140rpx;
		float: left;
	}
	.item-bottom-btn .item-btn-retreat{
		width: 200rpx;
		height: 88rpx;
		border: 2rpx solid #527FFF;
		background-color: #FFFFFF;
		color: #527FFF;
		font-size: 32rpx;
		font-weight: 700;
		text-align: center;
		line-height: 88rpx;
		margin: 24rpx 0 0 24rpx;
		border-radius: 44rpx;
		float: left;
	}
	.item-bottom-btn .item-btn-rob{
		width: 436rpx;
		height: 88rpx;
		/* height: calc( 100vh-100rpx ); */
		background-image: linear-gradient(#6B96FE,#4D84FE);
		color: #FFFFFF;
		font-size: 32rpx;
		font-weight: 700;
		text-align: center;
		line-height: 88rpx;
		margin: 24rpx 0 0 18rpx;
		border-radius: 44rpx;
		float: left;
	}
	.item-orders{
		width: 658rpx;
		float: left;
		margin: 10rpx 28rpx 10rpx 0;
		border-bottom: 1rpx solid #E1E1E1;
	}
	.item-orders .item-orders-line{
		margin: 0 28rpx 20rpx 32rpx;
		width: 598rpx;
		float: left;
	}
	.item-orders .item-orders-line .item-orders-line-title{
		color: #999999;
		float: left;
		font-size: 28rpx;
		text-align: left;
	}
	.item-orders .item-orders-line .item-orders-line-value{
		color: #191B1E;
		font-size: 28rpx;
		text-align: right;
	}
	.show-item-orders{
		height: 72rpx;
		line-height: 72rpx;
		width: 658rpx;
		text-align: center;
		float: left;
	}
	.show-item-orders-name{
		font-size: 28rpx;
		color: #999999;
		display: inline-block;
	}
	.s_img{
		width: 28rpx;
		height: 28rpx;
		margin-top: 22rpx;
		margin-left: 8rpx;
	}
	.noOrderGoods{
		word-wrap: break-word;
		word-break: normal;
	}
	.noOrderGoodsName{
		width: 594rpx!important;
	}
</style>
